export default {
  // 获取今天、昨天、明天
  getDate(dates) {
    var dd = new Date()
    var n = dates || 0
    dd.setDate(dd.getDate() + n)
    var y = dd.getFullYear()
    var m = dd.getMonth() + 1
    var d = dd.getDate()
    m = m < 10 ? '0' + m : m
    d = d < 10 ? '0' + d : d
    var day = y + '-' + m + '-' + d
    return day
  },
  // 获取本周、上周、下周的起始、结束日期
  getMonday(type, dates) {
    var now = new Date()
    var nowTime = now.getTime()
    var day = now.getDay()
    var longTime = 24 * 60 * 60 * 1000
    var n = longTime * 7 * (dates || 0)
    var dd = null
    if (type == 's') {
      dd = nowTime - (day - 1) * longTime + n
    }
    if (type == 'e') {
      dd = nowTime + (7 - day) * longTime + n
    }
    dd = new Date(dd)
    var y = dd.getFullYear()
    var m = dd.getMonth() + 1
    var d = dd.getDate()
    m = m < 10 ? '0' + m : m
    d = d < 10 ? '0' + d : d
    day = y + '-' + m + '-' + d
    return day
  },
  // 获取本月、上月、下月的起始、结束日期
  getMonth(type, months) {
    var d = new Date()
    var year = d.getFullYear()
    var month = d.getMonth() + 1
    if (Math.abs(months) > 12) {
      months = months % 12
    }
    if (months != 0) {
      if (month + months > 12) {
        year++
        month = (month + months) % 12
      } else if (month + months < 1) {
        year--
        month = 12 + month + months
      } else {
        month = month + months
      }
    }
    month = month < 10 ? '0' + month : month
    // var date = d.getDate()
    var firstday = year + '-' + month + '-' + '01'
    var lastday = ''
    if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {
      lastday = year + '-' + month + '-' + 31
    } else if (month == '02') {
      if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
        lastday = year + '-' + month + '-' + 29
      } else {
        lastday = year + '-' + month + '-' + 28
      }
    } else {
      lastday = year + '-' + month + '-' + 30
    }
    var day = ''
    if (type == 's') {
      day = firstday
    } else {
      day = lastday
    }
    return day
  },
  // 获取今年、去年、明年的开始、结束日期
  getYear(type, dates) {
    var dd = new Date()
    var n = dates || 0
    var year = dd.getFullYear() + Number(n)
    var day
    if (type == 's') {
      day = year + '-01-01'
    }
    if (type == 'e') {
      day = year + '-12-31'
    }
    if (!type) {
      day = year + '-01-01/' + year + '-12-31'
    }
    return day
  },
  //格式化日期 yyyy-MM-dd
  formartDate(dates){
    let date = dates ? dates : new Date()
    let yyyy = date.getFullYear();
    let MM = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)
    let dd = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
    return yyyy + '-' + MM + '-' + dd
  }
}
